	var arrId = [];
	var arrOldId = [];
	function SubmitForm(value)
	{
		tb_remove();
		$('#type_sumbit').val(value);
		$('#formList').submit();
	
	}
	/////////////////////////////////////////////////////////////
	function AddArticleFromOffer(id,module)
	{
		
		$('#ajaxGetOffer').show();
		$.get('/'+module+'/ajax_view/'+id,function(xml)
		{
			//Customer
			if($('#customer_code').val()=='')
			{
				fields = Array('id','code','name','address','email','taxid','tel','fax','zipcode','city','contact_person','dueday','delivery_address','delivery_city','delivery_zipcode','delivery_term')
				for (i in fields)
				{
					$("#customer_"+fields[i]).val($(xml).find("customer_"+fields[i]).text());	
				}
				$("#customer_remark").html($(xml).find("customer_remark").text());
				$("#customer_name2").html($(xml).find('customer_name').text());	
				$("#customer_name3").html($(xml).find('customer_name').text());	
				$("#customer_address2").html($(xml).find('customer_address').text());	
				$("#customer_zipcode_city2").html($(xml).find('customer_zipcode').text() +' '+ $(xml).find('customer_city').text());	
			}
			//Article
			arrId = [];	
			$(xml).find('article').each(function(){
				value = $(this).find('article_id').text();
				arrId[value] = [];
				arrId[value]['title'] = $(this).find('article_title').text();
				arrId[value]['desc'] = $(this).find('article_desc').text();
				arrId[value]['desc2'] = $(this).find('article_desc2').text();
				arrId[value]['remark'] = $(this).find('article_remark').text();
				arrId[value]['quantity'] = $(this).find('article_quantity').text();
				arrId[value]['price'] = $(this).find('article_price').text();
				arrId[value]['unit'] = $(this).find('article_unit').text();
				arrId[value]['discount']   = $(this).find('article_discount').text();
				arrId[value]['vat']   = $(this).find('article_vat').text();
				arrId[value]['total']   = $(this).find('article_total').text();
			});
			AddArticleaArrId();
			$('#ajaxGetOffer').hide();
		});
	}
	
	//////////////////////////////////////////////////////////////////
	function AddArticleaArrId()
	{
		for (id in arrId)
		{
			
			if (arrOldId[id] == null)
			{
				var strSelect = ListVAT(id,parseFloat(arrId[id]['vat']));
				data = '<tr class="smallfont" id="row_'+id+'">'
						+'<td align="center" >'
						+'<input type="hidden" name="article_id" value="'+id+'" />'
						+'<input id="article_title_'+id+'" type=hidden name="article['+id+'][title]" value="'+arrId[id]['title']+'">'
						
						+'<input id="article_desc2_'+id+'" type=hidden name="article['+id+'][desc2]" value="'+arrId[id]['desc2']+'">'
						+'<input id="article_unit_'+id+'" type=hidden name="article['+id+'][unit]" value="'+arrId[id]['unit']+'">'
							+arrId[id]['title']+'</td>'
						+'<td><input id="article_desc_'+id+'" type=text name="article['+id+'][desc]" value="'+arrId[id]['desc']+'"></td>'
						+'<td align="right"><input id="article_quantity_'+id+'" onchange="Cal(\'quantity\','+id+')" type=text name="article['+id+'][quantity]" size=3 value="'+arrId[id]['quantity']+'"></td>'
						+'<td align="right"><input size=5 id="article_price_'+id+'" onchange="Cal(\'price\','+id+')" type=text name="article['+id+'][price]" size=3 value="'+arrId[id]['price']+'"></td>'
						+'<td align="right"><input id="article_discount_'+id+'" onchange="Cal(\'discount\','+id+')" type=text name="article['+id+'][discount]" size=3 value="'+arrId[id]['discount']+'"></td>'
						+'<td align="right">'
							+strSelect+'</td>'
						+'</td>'
						+'<td align="right" id="article_total_'+id+'" class="float">'+arrId[id]['total']+'</td>'
						+'<td><input id="article_remark_'+id+'" type=text name="article['+id+'][remark]" value="'+arrId[id]['remark']+'"></td>'
						+'<td><a onclick="javascript:DeleteRow(\''+id+'\');return false;" href="#">Delete</a></td>'
					  +'</tr>';
				$('#articleTable').append(data);
				arrOldId[id] = arrId[id];
			}
			else
			{
				arrId[id] = null;
			}
		}
		CalAll();
		arrId = [];
	}
	//////////////////////////////////////////////////////////////////

	//////////////////////////////////////////////////////////////
	function DeleteRow(id)
	{
		$('#row_'+id).remove();
		arrOldId[id] = null;
		CalAll();
	}
	function Cal(name,id)
	{
		if($('#article_quantity_'+id).val() == 0)
			DeleteRow(id);	
		else
		{
			CalAll();
		}
	}
	function CalAll()
	{
		
		var net_id;
		var discount_id;
		var vat_id;
		var total_id;
		
		var item_net = 0;
		var item_discount =0;
		var item_vat = 0;
		var item_total = 0;
		var vat_group = Array();
		for (id in arrOldId)
		{
			if(arrOldId[id]!= null)
			{
				net_id     	 = parseFloat($('#article_quantity_'+id).val()) * parseFloat($('#article_price_'+id).val()) * (1-parseFloat($('#article_discount_'+id).val())/100);

				vat_id       = net_id*parseFloat($('#article_vat_'+id).val())/100;
				total_id     = net_id  + vat_id;
				$('#article_total_'+id).html(net_id.toFixed(2));
				item_net 		+= net_id;
				item_vat 		+= vat_id;
				item_total 		+= total_id;
				//Cal VAT Group
				if(vat_group[$('#article_vat_'+id).val()]==null)
					vat_group[$('#article_vat_'+id).val()] = 0;
				vat_group[$('#article_vat_'+id).val()] += vat_id;
			}
		}
		//Print VAT Group
		var str_vat_group = '';
		for (i in vat_group)
		{
			if (i != '0')
				str_vat_group += '<tr><td><b>VAT '+i+'%:</b></td><td align="right" class="float">'+vat_group[i].toFixed(2)+'</td></tr>';
		}
		str_vat_group = '<table cellpadding="2" width="200" class="smallfont" border="0">'
						+str_vat_group
						+'</table>';
		$('#vat_group').html(str_vat_group);
		$('#item_net').html(item_net.toFixed(2));
		$('#item_vat').html(item_vat.toFixed(2));
		$('#item_total').html(item_total.toFixed(2));	
		$('#item_total2').html(item_total.toFixed(2));	
		$('#item_total3').html(item_total.toFixed(2));
		FormatNumber();
	}
	function ListVAT (id,checked)
	{
		var  string = '<select size="1" id="article_vat_'+id+'" name="article['+id+'][vat]" onchange="Cal(\'vat\','+id+')" >';
		var arrVAT = new Array(0,8,14,25);
		
		for (i in arrVAT)
		{
			key = arrVAT[i];
			value = arrVAT_Name[i];
			if (key == checked)
				string += '<option value="'+key+'" selected="true">'+value+'%</option>';
			else
				string += '<option value="'+key+'" >'+value+'%</option>';
		}
		string += '</select>';
		return string;
	}